package com.bsh.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bsh.api.Exception.ApiException;
import com.bsh.service.ApiUserBankService;
import com.bsh.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 用户银行记录相关API
 * 
 * 
 */
@Controller
@RequestMapping(value = "/api/v1/bank", method = RequestMethod.POST)
public class ApiUserBankController {
	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	HttpSession session;
	@Autowired
	ApiUserBankService apiUserBankService;


	/**
	 * 添加用户银行
	 * 
	 * @param
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/add", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage saveUserBank(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("serv_ip", ApiUtils.getUrl(request));
			msg = apiUserBankService.saveUserBank(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}

	}

	/**
	 * 查询可用的银行卡列表
	 * 
	 * @param
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/allCardList", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage queryBankList(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("serv_ip", ApiUtils.getUrl(request));
			msg = apiUserBankService.queryBankList(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}

	/**
	 * 查询我的银行卡
	 * 
	 * @param
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/myCardList", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage queryMyBankList(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("serv_ip", ApiUtils.getUrl(request));
			msg = apiUserBankService.queryMyBankList(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}

	/**
	 * 删除用户银行
	 * 
	 * @param
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public ApiMessage delUserBank(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("serv_ip", ApiUtils.getUrl(request));
			msg = apiUserBankService.delUserBank(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}

}
